Ti.include('constants.js');
Ti.include('stringbuilder.js');

//----- UI elementen
var messageView = Titanium.UI.createView({
   id : 'messageview',
   height : 40,
   width : 250,
   borderRadius : 10,
   backgroundColor : hh_donkerblauw,
   opacity : 0.7,
   touchEnabled : false
});

if (isIphone()) {
   messageView.setBottom(30);
} 
else if (isIpad()) {
   messageView.setBottom(70);
}

var messageLabel = Titanium.UI.createLabel({
   id : 'messagelabel',
   text : '',
   color : wit,
   width : 250,
   height : 'auto',
   font : {
      fontFamily : 'Helvetica Neue',
      fontWeight : 'bold',
      fontSize : 13
   },
   textAlign : 'center'
});



//----------------------------------------------------------------------------
// Functions
//----------------------------------------------------------------------------
function isIpad() {
   return (Ti.Platform.osname === "ipad") ;
}

function isIphone() {
   return (Ti.Platform.osname === "iphone") ;
}

function isAndroid() {
   return (Ti.Platform.osname === "android") ;
}

function isIOS7Plus() {
   if (Titanium.Platform.name === 'iPhone OS') {
      var version = Titanium.Platform.version.split(".");
      var major = parseInt(version[0], 10);
      if (major >= 7) {
         return true;
      }
   }
   return false;
}

function getCurrentLocation() {
   /* returned JSON object should look like this:
    {
    "accuracy": 100,
    "altitude": 0,
    "altitudeAccuracy": null,
    "heading": 0,
    "latitude": 40.493781233333333,
    "longitude": -80.056671
    "speed": 0,
    "timestamp": 1318426498331
    }
    */
   if (!Ti.Geolocation.locationServicesEnabled) {
      alert('Activeer aub locatie voorzieningen');
      return null;
   }

   //IOS only
   Titanium.Geolocation.purpose = 'Huidige positie bepalen';
   Titanium.Geolocation.getCurrentPosition(function(e) {
      if (e.error) {
         Ti.API.error('Fout: ' + e.error);
         return null;
      }
      Ti.API.info('Locatie: ' + e.coords);
      return e.coords;
   });

}

function getDate() {
   var currentTime = new Date();
   var hours = currentTime.getHours();
   var minutes = currentTime.getMinutes();
   var seconds = currentTime.getSeconds();
   var month = currentTime.getMonth() + 1;
   var day = currentTime.getDate();
   var year = currentTime.getFullYear();

   if (hours < 10) {
      hours = "0" + hours;
   };
   if (minutes < 10) {
      minutes = "0" + minutes;
   };
   if (seconds < 10) {
      seconds = "0" + seconds;
   };

   return month + "-" + day + "-" + year + "-" + hours + ":" + minutes + ":" + seconds;
}

function getReversedDate() {
   var dt = new Date();
   return dt.getFullYear() +dt.getMonth() +dt.getDate() +dt.getHours()+dt.getMinutes()+dt.getSeconds();
}

function log(msg) {
   Ti.API.info(getDate() + " - " + msg);
}

function hasInternetConnection() {
   return (Titanium.Network.networkType === Titanium.Network.NETWORK_NONE);
}

function createEmail() {
   var emailDialog = Ti.UI.createEmailDialog();
   emailDialog.subject = 'Heerderhof app - Info';
   emailDialog.toRecipients = ['info@heerderhof.nl'];
   emailDialog.open();   
   emailDialog.addEventListener('complete', function(event) {
      if (event.result == emailDialog.SENT) {
         log("Info bericht verzonden");
         Ti.Analytics.featureEvent(APP_EVENTS.EVENT_INFO_FORM_SENT);
      }
      else if (event.result == emailDialog.CANCELLED) {
         log("Bericht versturen geannuleerd");
      }
      else {
         log("Onbekende fout opgetreden: " + event.toString);
      }
   });   
}

function showSystemInformation() {
   var sb = new StringBuilder();
   sb.append('HARDWARE INFORMATION\n');
   sb.append('----------------------------------------\n');
   sb.append('Device       : ' +Ti.Platform.model +'\n');
   sb.append('Manufacturer : ' +Ti.Platform.manufacturer +'\n');
   sb.append('Name         : ' +Ti.Platform.name +'\n');
   sb.append('Architecture : ' +Ti.Platform.architecture +'\n');
   sb.append('IP address   : ' +Ti.Platform.address +'\n');
   sb.append('Mac address  : ' +Ti.Platform.macaddress +'\n');
   sb.append('Netmask      : ' +Ti.Platform.netmask +'\n');
   sb.append('Processors   : ' +Ti.Platform.processorCount +'\n');
   sb.append('\n\n');   
   sb.append('SOFTWARE INFORMATION\n');
   sb.append('----------------------------------------\n');
   sb.append('Platform     : ' +Ti.Platform.name +'\n');
   sb.append('OS Version   : ' +Ti.Platform.version +'\n');   
   sb.append('OS Name      : ' +Ti.Platform.osname +'\n');
   sb.append('JS runtime   : ' +Ti.Platform.runtime +'\n');
   
   var sysInfo = sb.toString();
   Ti.API.info(sysInfo);
}
